/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 8 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.ui.android;

import org.betonthemove.platform.*;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.app.Dialog;
import android.app.ProgressDialog;

public class FullEventTypeSelection extends ListActivity implements Runnable {

	private static final int WAITING_DIALOG = 1;
    private static final int ERROR_DIALOG = 2;
    private static final int ERROR_MESSAGE_DISPLAY_DELAY = 4000;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        java.util.List<String> list=new java.util.Vector<String>();
        
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list));
        getListView().setTextFilterEnabled(true);
        
        runOnUiThread(this);
    }
    
    public void run() {
    	
    	if (m_state == 0) {
    		showDialog(WAITING_DIALOG);
    		
    		m_state++;
    		
    		new Thread(this).start();
    	} else if (m_state == 1) {
	        
	        m_state++;

    		BettingPlatform platform=BettingPlatformFactory.getBettingPlatform();
        
	        try {
		        m_eventTypes=platform.getEventTypes();
		        
	        } catch(Exception e) {
	        	m_error = e.getLocalizedMessage();
	        	
	        	m_state = 5;
	        }

        	runOnUiThread(this);
    	} else if (m_state == 2) {
    		
	        for (int i=0; i < m_eventTypes.size(); i++) {
	        	((ArrayAdapter<String>)getListAdapter()).add(m_eventTypes.get(i).getName());
	        }
	        
	        dismissDialog(WAITING_DIALOG);
    		
	        m_state = -1;
    		
    	} else if (m_state == 5) {

    		dismissDialog(WAITING_DIALOG);
	        
	        showDialog(ERROR_DIALOG);
	        
	        m_state++;
	        
	        new Thread(this).start();
	        
    	} else if (m_state == 6) {

    		try {
    			synchronized(this) {
    				wait(ERROR_MESSAGE_DISPLAY_DELAY);
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		
    		m_state++;
    		
    		runOnUiThread(this);
    		
    	} else if (m_state == 7) {
    		
    		dismissDialog(ERROR_DIALOG);
    		
    		m_error = null;
    		
    		if (m_eventTypes == null) {
    			finish();
    		} else {
    			// Go back to refresh state
    			m_state = 2;
    			
        		runOnUiThread(this);
    		}
    	}
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case WAITING_DIALOG: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("Retrieving event types...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(true);
                return dialog;
            }
            case ERROR_DIALOG: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("ERROR: "+m_error);
                dialog.setIndeterminate(true);
                dialog.setCancelable(true);
                return dialog;
            }
        }
        return null;
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent();
        intent.setClass(FullEventTypeSelection.this, MarketSelection.class);
        
        intent.putExtra(EventTypeSelection.EVENT_TYPE_ID_ATTR,
        			m_eventTypes.get(position).getId());
        
        startActivity(intent);
	}
    
	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
	    return(OptionsMenuSupport.onCreateOptionsMenu(menu));
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    return(OptionsMenuSupport.onOptionsItemSelected(FullEventTypeSelection.this, item));
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);		
	}

	private java.util.List<EventType> m_eventTypes=null;
    
    private int m_state=0;
	private String m_error=null;
}